layui.use(['form','layer'],function(){
    var form = layui.form(),
        layer = parent.layer === undefined ? layui.layer : parent.layer,
        $ = layui.jquery;

    // 发送短信验证码
    $('.sendms-btn').on('click', sendms);
    function sendms() {
        var tel = $('[name="tel"]').val();
        if (tel == '') {
            layer.msg('请输入手机号');
            return;
        }
        var index = top.layer.msg('短信发送中',{icon: 16,time:false,shade:0.8});
        $.ajax({
            url: '/sendms',
            type: 'POST',
            dataType: 'json',
            timeout: 5000,
            data: {tel: tel},
            complete: function(){
                layer.close(index);
            },
            error: function(){
                layer.alert('参数错误', {icon: 2}, function(index){
                    layer.close(index);
                });
            },
            success: function(res){
                if (res.status != 0) {
                    layer.msg(res.msg);
                } else {
                    layer.msg("短信已发送");
                }
            }
        });

        $('.sendms-btn').off();
        $('.sendms-btn').removeClass('layui-btn-warm').addClass('layui-btn-disabled').text('(60)');
        var limitTime = 60;
        var dingshi = setInterval(function(){
            if (limitTime > 0) {
                --limitTime;
                $('.sendms-btn').text('('+limitTime+')');
            } else {
                clearInterval(dingshi);
                $('.sendms-btn').removeClass('layui-btn-disabled').addClass('layui-btn-warm').text('再次获取').on('click', sendms);
            }

        }, 1000);
        return false;
    }

    //登录按钮事件
    form.on("submit(login)",function(data){
        $.ajax({
            url: '/login',
            type: 'POST',
            dataType: 'json',
            timeout: 4000,
            data: data.field,
            error: function(){
                layer.alert('参数错误', {icon: 2}, function(index){
                    layer.close(index);
                    $('[name="code"]').val('');
                });
            },
            success: function(res){
                if (res.status != 0) {
                    layer.alert(res.msg, {icon: 2}, function (index) {
                        layer.close(index);
                        $('[name="code"]').val('');
                    })
                    return false;
                }
                top.location.href = '/index';
                layer.msg('登录成功,正在为您跳转');
            }
        });
        return false;
    })
})